package com.jkm.controller;

import com.jkm.dto.UserInsertDTO;
import com.jkm.dto.UserUpdateDTO;
import com.jkm.log.Log;
import com.jkm.model.User;
import com.jkm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("user")
@Log("用户相关")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/insert",method = RequestMethod.POST)
    @Log("controller用户保存")
    public String insert(@RequestBody UserInsertDTO userInsertDTO){
        int save = userService.save(userInsertDTO);
        String str;
        if(save==1) {
            str = "保存成功";
        }else {
            str = "保存失败";
        }
        return str;
    }

    @RequestMapping(value = "/selectById/{id}",method = RequestMethod.GET)
    @Log("controller用户根据id查找")
    public User selectById(@PathVariable Long id){
        User user = userService.selectById(id);
        return user;
    }

    @RequestMapping(value = "/del/{id}",method = RequestMethod.GET)
    @Log("controller用户删除")
    public String del(@PathVariable Long id){
        userService.del(id);
        return "执行完成";
    }

    @RequestMapping(value = "update",method = RequestMethod.POST)
    @Log("controller用户更新")
    public String updateUser(@RequestBody UserUpdateDTO userUpdateDTO){
        int i = userService.updateUser(userUpdateDTO);
        String str;
        if(i==1) {
            str = "更新成功";
        }else {
            str = "更新失败";
        }
        return str;
    }

    @Log("controller用户列表")
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public List<User> list(){
        return userService.selectAll();
    }

}
